package in.iqing.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.base.BaseActivity;
import in.iqing.control.adapter.BookListAdapter;
import in.iqing.control.api.callback.BookListCallback;
import in.iqing.control.helper.IntentHelper;
import in.iqing.control.util.ToastUtil;
import in.iqing.model.bean.Book;
import in.iqing.view.widget.SimpleDividerItemDecoration;

/**
 * Created by ZuoShu on 15/7/7.
 */
public abstract class BaseBookListActivity extends BaseActivity {
    protected abstract void requestBookList(int pageSize, int pageNo, BookListCallback callback);
    protected abstract void initializeVariable();

    @Bind(R.id.ultimate_recycler_view)
    UltimateRecyclerView recyclerView;

    @Bind(R.id.title_text)
    TextView title;

    private BookListAdapter listAdapter;
    private int pageSize = 10;
    private int pageNo = 1;
    private BookListCallback firstLoadCallback;
    private BookListCallback loadMoreCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_list);
    }

    @Override
    protected void onInitialize(Bundle savedInstanceState) {
        super.onInitialize(savedInstanceState);
        firstLoadCallback = new FirstLoadCallback();
        loadMoreCallback = new LoadMoreCallback();
        setupRecyclerView();
        initializeVariable();
        loadBookList();
    }

    private void setupRecyclerView() {
        listAdapter = new BookListAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(listAdapter);
        recyclerView.enableLoadmore();
        recyclerView.setOnLoadMoreListener(new ExtLoadMoreListener());
        listAdapter.setCustomLoadMoreView(View.inflate(this, R.layout.widget_loadmore, null));
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getContext()));
        listAdapter.setListener(new ExtOnBookItemClickListener());
    }

    private void loadBookList() {
        pageNo = 1;
        requestBookList(pageSize, pageNo, firstLoadCallback);
    }

    private void loadMoreCategory() {
        pageNo++;
        requestBookList(pageSize, pageNo, loadMoreCallback);
    }

    @OnClick(R.id.back)
    public void onBackClick(View v) {
        finish();
    }

    class FirstLoadCallback extends BookListCallback {
        @Override
        public void onStart() {
            showLoading();
            listAdapter.clear();
        }

        @Override
        public void onFailure(int code, String msg) {
            showEmpty();
        }

        @Override
        public void onSuccess(List<Book> bookList) {
            if (bookList == null || bookList.size() == 0) {
                showEmpty();
                return;
            }
            showContent();
            listAdapter.add(bookList);
            listAdapter.notifyDataSetChanged();
        }
    }

    class LoadMoreCallback extends BookListCallback {

        @Override
        public void onFailure(int code, String msg) {
            ToastUtil.show(getContext(), R.string.common_empty);
            listAdapter.notifyDataSetChanged();
        }

        @Override
        public void onSuccess(List<Book> bookList) {
            if (bookList == null || bookList.size() == 0) {
                ToastUtil.show(getContext(), R.string.common_empty);
                // TODO dismiss loading more
            } else {
                listAdapter.add(bookList);
            }
            listAdapter.notifyDataSetChanged();
        }
    }

    class ExtLoadMoreListener implements UltimateRecyclerView.OnLoadMoreListener {

        @Override
        public void loadMore(int itemsCount, int maxLastVisiblePosition) {
            loadMoreCategory();
        }
    }

    class ExtOnBookItemClickListener implements BookListAdapter.OnItemClickListener {

        @Override
        public void onItemClick(Book book) {
            Bundle data = new Bundle();
            data.putSerializable("book", book);
            IntentHelper.startActivity(BaseBookListActivity.this, BookActivity.class, data);
        }
    }


}
